﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using RazakiControls.Classes;

namespace RazakiControls
{
    [DefaultProperty("Caption")]
    [ToolboxData("<{0}:RazakiTextBox runat=server></{0}:RazakiTextBox>")]
    [ToolboxBitmap(typeof(RazakiTextBox), "Images.DefaultControl.ico")]
    public class RazakiTextBox : TextBox
    {
        private readonly Label _label = new Label();

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Caption
        {
            get
            {
                var caption = (string)ViewState["Caption"];
                return (caption ?? String.Empty);
            }
            set
            {
                ViewState["Caption"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool DirectionToRight
        {
            get
            {
                var isRtl = true;

                if (ViewState["DirectionToRight"] != null)
                    isRtl = bool.Parse(ViewState["DirectionToRight"].ToString());

                return isRtl;
            }
            set
            {
                ViewState["DirectionToRight"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public InputType Format
        {
            get
            {
                InputType result = InputType.Text;

                if (ViewState["Format"] != null)
                    result = (InputType) Enum.Parse(typeof (InputType), ViewState["Format"].ToString());

                return result;
            }
            set
            {
                ViewState["Format"] = value;
            }
        }


        protected override void OnInit(EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(Caption))
            {
                _label.Width = Utilities._CAPTION_WIDTH;
                _label.Text = string.Format("{0}: ", Caption);

                Controls.Add(_label);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //begin #1
            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "inline-block");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            //#2
            if (!string.IsNullOrWhiteSpace(Caption))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "ContentTop");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                _label.RenderControl(writer);

                writer.RenderEndTag();//td
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            //#3
            writer.AddStyleAttribute(HtmlTextWriterStyle.Direction, DirectionToRight ? "rtl" : "ltr");
            SetFormat(writer);
            base.Render(writer);

            writer.RenderEndTag();//td
            
            //end #1
            writer.RenderEndTag();//tr
            writer.RenderEndTag();//table
        }

        private void SetFormat(HtmlTextWriter writer)
        {
            switch (Format)
            {
                case InputType.Text:
                    //nothing
                    break;
                case InputType.Number:
                    writer.AddAttribute("onkeyup", "javascript: RemoveNotNumber(this);");
                    break;
                case InputType.NumberSeperated:
                    writer.AddAttribute("onkeyup", "javascript: SeperateDigits(this);");
                    break;
            }
        }

    }
}
